import { createRouter, createWebHistory } from 'vue-router'
import LayoutIndex from "@/views/Layout/index.vue";
import LoginIndex from "@/views/Login/index.vue";
import HomeIndex from "@/views/Home/index.vue";
import DetailIndex from "@/views/Detail/index.vue";
import OrderIndex from "@/views/Order/index.vue";
import OrderDisplayIndex from "@/views/OrderDisplay/index.vue";
import PersonalHome from "@/views/PersonalHome/index.vue";
import PersonalInfo from "@/views/PersonalHome/componets/PersonalInfo.vue";
import DeliveryAddress from "@/views/PersonalHome/componets/DeliveryAddress.vue";
import Pay from "@/views/Order/components/Pay.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: LayoutIndex,
      children: [
        {
          path: '',
          component: HomeIndex
        }
        ,
        {
          path: 'detail/:id',
          component: DetailIndex
        },
        {
          path: 'pay',
          component: Pay
        },
        {
          path: 'order',
          component: OrderIndex
        },
        {
          path: 'orderdisplay',
          component: OrderDisplayIndex
        },
        {
          path: 'personal',
          component: PersonalHome,
          children: [
            {
              path: '',
              component: PersonalInfo
            },
            {
              path: 'address',
              component: DeliveryAddress
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: LoginIndex
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (localStorage.getItem('adminTokenName')) {
      next()
    } else {
      next("/login")
    }
  }
  window.scrollTo(0, 0)
})


export default router
